/**
 * 通过WebSocket推送8个实时数据到客户端
 */
package cn.web.tunnelrobot.websocket;

import cn.web.tunnelrobot.StartRunning;
import cn.web.tunnelrobot.controller.WebSocketServer;
import cn.web.tunnelrobot.dto.RealTimeData;
import cn.web.tunnelrobot.util.Constants;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import lombok.SneakyThrows;
import java.io.IOException;


@Slf4j
@Component  // 被Spring容器管理
@Order(2)   // 如果多个自定义ApplicationRunner，用来表明执行顺序
public class PushAlarm {

    public static void myTimer() {
        Runnable runnable1 = new Runnable() {
            Logger logger = LoggerFactory.getLogger(PushAlarm.class);
            @SneakyThrows
            @Override
            public void run() {
                while (true) {

                    RealTimeData realTimeData = Constants.realTimeData;
                    try {
                        WebSocketServer.sendInfo(JSONArray.toJSONString(realTimeData)); // 推送
                    } catch (IOException e) {
//                        e.printStackTrace();
                        logger.error("未连接机器人");
                    }
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        logger.error("未连接机器人");
                    }
                }
            }
        };
        Thread thread1 = new Thread(runnable1);
        thread1.start();
    }
}
